
package id.isran.kamusindonesiamuna.fragment;

import id.isran.kamusindonesiamuna.R;
import id.isran.kamusindonesiamuna.basisdata.Data;
import id.isran.kamusindonesiamuna.utils.Utils;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class FragmentKataMunaIna extends Fragment implements OnClickListener {

    private Button btn_terjemahkan_muna;
    private AutoCompleteTextView autoCompleteTextMuna;
    private TextView txt_kata_dicari_muna;
    private TextView txt_kata_terjemahan_muna;
    private ArrayAdapter<String> autoCompleteAdapterMuna;
    private String[] dataAutoCompleteMuna;
    private ImageView btn_play;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Utils.TRACE("FragmentKataMunaIna", "onCreate: munaIna");
        // Data.initData(getActivity(), Data.MUNA_INDO);
        loadData();
    }

    private void loadData() {
        dataAutoCompleteMuna = new String[Data.BARIS_COUNT_MUNA_INA + 1];
        for (int i = 0; i < Data.BARIS_COUNT_MUNA_INA + 1; i++) {
            dataAutoCompleteMuna[i] = Data.dataMunaIna[i][1];
            Utils.TRACE("FragmentKataMunaIna", "loadData: " + Data.dataMunaIna[i][0] + "-"
                    + Data.dataMunaIna[i][1]);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Utils.TRACE("FragmentKataMunaIna", "onCreateView: ");
        ViewGroup view = (ViewGroup) inflater.inflate(
                R.layout.fragment_kata_muna, null);

        autoCompleteTextMuna = (AutoCompleteTextView) view.findViewById(R.id.et_search_kata_muna);
        txt_kata_dicari_muna = (TextView) view.findViewById(R.id.txt_kata_dicari_muna);
        txt_kata_terjemahan_muna = (TextView) view.findViewById(R.id.txt_kata_terjemahan_muna);
        btn_terjemahkan_muna = (Button) view.findViewById(R.id.btn_terjemahkan_kata_muna);
        btn_play = (ImageView) view.findViewById(R.id.btnPlayKataMuna);

        btn_terjemahkan_muna.setOnClickListener(this);
        btn_play.setOnClickListener(this);

        autoCompleteAdapterMuna = new ArrayAdapter<String>(getActivity().getApplicationContext(),
                R.layout.single_item_dropdown_autocomplete, dataAutoCompleteMuna);
        autoCompleteTextMuna.setAdapter(autoCompleteAdapterMuna);

        return view;
    }

    @Override
    public void onClick(View v) {
        if (v == btn_terjemahkan_muna) {
            if (!TextUtils.isEmpty(autoCompleteTextMuna.getText().toString())) {
                txt_kata_dicari_muna.setText("" + autoCompleteTextMuna.getText().toString());
                txt_kata_terjemahan_muna.setText(""
                        + Data.cariKata(autoCompleteTextMuna.getText().toString().toLowerCase(),
                                Data.MUNA_INDO));
            } else {
                Toast.makeText(getActivity(), "Isikan kata yang ingin dicari", Toast.LENGTH_SHORT)
                        .show();
            }
        } else if (v == btn_play) {
            if (!TextUtils.isEmpty(autoCompleteTextMuna.getText().toString())) {
                Utils.playAudio(getActivity(),
                        Utils.SOUND_MUNA + "" + autoCompleteTextMuna.getText());
            }
        }
    }
}
